<%@ page language="java" import="java.util.*,shop.dao.*,shop.entity.*"  pageEncoding="utf-8"%>
<%
	String userName = request.getParameter("userName");
	String password = request.getParameter("password");
    String Competence = request.getParameter("Competence");
    
	AdminDao adminDao = new AdminDao();
    CustomerDao customerDao = new CustomerDao();

	if (userName == null || password == null) {
		out.print("<script type='text/javascript'>alert('请输入帐号和密码。');history.go(-1);</script>");
	} else {
		Admin admin = adminDao.findadmin(userName, password);
		Customer customer = customerDao.findcustomer(userName, password);
		
		if (admin != null && Competence.equals("admin")) {	
		        session.setAttribute("lasttime", admin.getAl_time());   
		        int timp=adminDao.updateLoginTime(admin.getA_ID());  
		        if(timp!=0){   
				session.setAttribute("userSession", "管理员");
				session.setAttribute("username", userName);
				session.setAttribute("userID", admin.getA_ID());
				response.sendRedirect("../index.jsp");
				}
				else{
				out.print("<script type='text/javascript'>alert('更新时间出错！');history.go(-1);</script>");
				}
	    }else if (customer != null && Competence.equals("customer")){
	    		session.setAttribute("lasttime", customer.getL_time());   
		        int timp1=customerDao.updateLoginTime(customer.getC_ID());  
		        if(timp1!=0){ 
	    		session.setAttribute("userSession", "客户");
				session.setAttribute("username", userName);
				session.setAttribute("userID",customer.getC_ID());
				response.sendRedirect("../index.jsp");
				}
				else{
				out.print("<script type='text/javascript'>alert('更新时间出错！');history.go(-1);</script>");
				}
		} else {
			out.print("<script type='text/javascript'>alert('登录失败,帐号或密码错误。');history.go(-1);</script>");
		}
	}
	
%>
